package com.clkj.xiuzheng.service;

import com.clkj.xiuzheng.dao.CompanyMapper;
import com.clkj.xiuzheng.pojo.Company;
import com.clkj.xiuzheng.pojo.Customer;
import com.clkj.xiuzheng.pojo.Warehouse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class CompanyServiceImpl implements CompanyService{

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public void addCompany(Company company) {
        // 创建一个Random对象
        Random random1 = new Random();
//        Random random2 = new Random();

        // 生成6位的随机数
        int min = 100000; // 最小值是100000（包括）
        int max = 999999; // 最大值是999999（包括）
        int account = random1.nextInt(max - min + 1) + min;
//        int password = random2.nextInt(max - min + 1) + min;
        company.setCompanyAccount(String.valueOf(account));
        company.setCompanyPassword("123456");
        companyMapper.insertSelective(company);
    }

    @Override
    public void updateCompanyDown(Integer companyId,Integer status)
    {
        Company company = new Company();
        company.setCompanyId(companyId);
        company.setCompanyStatus(status);
        companyMapper.updateByPrimaryKeySelective(company);
        company = null;
    }

    @Override
    public void updateCompanyUp(Integer companyId) {
        companyMapper.updateCompanyUp(companyId);
    }

    @Override
    public PageInfo listCompany(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Company> companies = companyMapper.listCompany();
        return new PageInfo(companies);
    }

    @Override
    public PageInfo getCompanyByName(Integer pageSize, Integer pageNum, String companyByName) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("company_name like '%"+companyByName+"%'");
        return new PageInfo<>(companyMapper.selectByExample(example));
    }

    @Override
    public Map companyLogin(String account, String password) {
        Example example =  new Example(Company.class);
        Example.Criteria criteria =  example.createCriteria();
        criteria.andEqualTo("companyAccount",account).andEqualTo("companyPassword",password);
        Company company = companyMapper.selectOneByExample(example);
        Map map = new HashMap();
        if(company==null){
            map.put("flag", "error");
            map.put("msg","账号或密码错误");
            return map;

        }
        if(company.getCompanyStatus()==0){
            map.put("flag", "error");
            map.put("msg","该账号已被禁用，请联系管理员");
            return map;
        }
        map.put("flag", "success");
        map.put("company",company);
        return map;
    }


}
